home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1994 / MacHack 1994.toast / MacHack™ 1987-1994 / MacHack™ '90 / MacHack'90 Proceedings / John Norstad / Reusable Code / Source / wstm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-10  |  4.9 KB  |  173 lines  |  [TEXT/MPS ]

  1. /*______________________________________________________________________
  2.  
  3.     wstm.c - Window State Module
  4.     
  5.     Copyright © 1988, 1989, 1990 Northwestern University.  Permission is 
  6.     granted to use this code in your own projects, provided you give credit 
  7.     to both John Norstad and Northwestern University in your about box or 
  8.     document.
  9.     
  10.     This module keeps track of window states.
  11.     
  12.     All of the code is placed in its own segment named "wstm".
  13. _____________________________________________________________________*/
  14.  
  15.  
  16. #pragma load "precompile"
  17. #include "utl.h"
  18. #include "rez.h"
  19. #include "glob.h"
  20. #include "wstm.h"
  21.  
  22. #pragma segment wstm
  23.  
  24. /*______________________________________________________________________
  25.  
  26.     wstm_Init - Initialize Window State.
  27.     
  28.     Entry:    windState = pointer to WindState struct.
  29.                     
  30.     Exit:        window state information initialized.
  31. _____________________________________________________________________*/
  32.  
  33.  
  34. void wstm_Init (WindState *windState)
  35.  
  36. {
  37.     windState->moved = false;;
  38. }
  39.  
  40. /*______________________________________________________________________
  41.  
  42.     wstm_Save - Save Window State.
  43.     
  44.     Entry:    theWindow = window pointer.
  45.                 windState = pointer to WindState struct.
  46.                     
  47.     Exit:        window state information saved in WindState structure.
  48. _____________________________________________________________________*/
  49.  
  50.  
  51. void wstm_Save (WindowPtr theWindow, WindState *windState)
  52.  
  53. {
  54.     if (windState->moved) {
  55.         utl_SaveWindowPos(theWindow, &windState->userState, &windState->zoomed);
  56.     };
  57. }
  58.  
  59. /*______________________________________________________________________
  60.  
  61.     wstm_ComputeStd - Compute a Window's Standard State.
  62.     
  63.     Entry:    theWindow = pointer to window.
  64.     
  65.     Exit:        computed standard state stored in state data block.
  66.     
  67.     See TN 79.
  68. _____________________________________________________________________*/
  69.  
  70.  
  71. void wstm_ComputeStd (WindowPtr theWindow)
  72.  
  73. {
  74.     WindowPeek        w;                    /* window pointer */
  75.     WStateData        **stateData;    /* handle to zoom state data */
  76.     GDHandle            gd;                /* handle to gdevice containing window */
  77.     Rect                zoomRect;        /* rect to zoom to */
  78.     Rect                windRect;        /* window rect in global coords */
  79.     Boolean            hasMB;            /* true if window contains menu bar */
  80.     short                windWidth;        /* width of window */
  81.     
  82.     w = (WindowPeek)theWindow;
  83.     if (!(w->dataHandle && w->spareFlag)) return;
  84.     utl_GetWindGD(theWindow, &gd, &zoomRect, &windRect, &hasMB);
  85.     zoomRect.top += titleBarHeight + zoomSlop + 
  86.         (hasMB ? utl_GetMBarHeight() : 0);
  87.     zoomRect.bottom -= 3;
  88.     stateData = (WStateData**)w->dataHandle;
  89.     windWidth = windRect.right - windRect.left;
  90.     if (zoomRect.left <= windRect.left &&
  91.         windRect.right <= zoomRect.right) {
  92.         zoomRect.left = (**stateData).userState.left;
  93.         zoomRect.right = zoomRect.left + windWidth;
  94.     } else if (windRect.right > zoomRect.right) {
  95.         zoomRect.right -= 3;
  96.         zoomRect.left = zoomRect.right - windWidth;
  97.     } else {
  98.         zoomRect.left += 3;
  99.         zoomRect.right = zoomRect.left + windWidth;
  100.     };
  101.     (**stateData).stdState = zoomRect;
  102. }
  103.  
  104. /*______________________________________________________________________
  105.  
  106.     wstm_ComputeDef - Compute a Window's Default State.
  107.     
  108.     Entry:    theWindow = pointer to window.
  109.     
  110.     Exit:        userState = computed state rectangle (location and size).
  111. _____________________________________________________________________*/
  112.  
  113.  
  114. void wstm_ComputeDef (WindowPtr theWindow, Rect *userState)
  115.  
  116. {
  117.     Point            pos;            /* window position */
  118.     
  119.     *userState = theWindow->portRect;
  120.     utl_StaggerWindow(userState, staggerInitialOffset, staggerOffset, &pos);
  121.     OffsetRect(userState, pos.h, pos.v);
  122. }
  123.  
  124. /*______________________________________________________________________
  125.  
  126.     wstm_Restore - Restore Window State.
  127.     
  128.     Entry:    dlog = true if dialog window, false if regular window.
  129.                 windID = resource ID of window template.
  130.                 wStorage = pointer to window storage, or nil.
  131.                 windState = pointer to saved window state.
  132.                     
  133.     Exit:        function result = pointer to window, positioned and sized.
  134. _____________________________________________________________________*/
  135.  
  136.  
  137. WindowPtr wstm_Restore (Boolean dlog, short windID, Ptr wStorage,
  138.     WindState *windState)
  139.  
  140. {
  141.     WindowPtr        theWindow;            /* pointer to window */
  142.     Rect                userState;            /* user state rectangle */
  143.     
  144.     theWindow = dlog ?
  145.         GetNewDialog(windID, wStorage, (WindowPtr)-1) :
  146.         utl_GetNewWindow(windID, wStorage, (WindowPtr)-1);
  147.     if (!windState->moved) {
  148.         wstm_ComputeDef(theWindow, &userState);
  149.         MoveWindow(theWindow, userState.left, userState.top, false);
  150.         SizeWindow(theWindow, userState.right-userState.left,
  151.             userState.bottom-userState.top, true);
  152.     } else {
  153.         utl_RestoreWindowPos(theWindow, &windState->userState, windState->zoomed,
  154.             dragSlop, wstm_ComputeStd, wstm_ComputeDef);
  155.     };
  156.     return theWindow;
  157. }
  158.  
  159. /*______________________________________________________________________
  160.  
  161.     wstm_Mark - Mark a Window Moved or Sized.
  162.     
  163.     Entry:    windState = pointer to saved window state.
  164. _____________________________________________________________________*/
  165.  
  166.  
  167. void wstm_Mark (WindState *windState)
  168.  
  169. {
  170.     windState->moved = true;
  171. };
  172.  
  173.